﻿/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 *
 *  This code is provided on an AS IS basis, with no WARRANTIES,
 *  CONDITIONS or GUARANTEES of any kind.
 *
 **/

using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Peter.Common;
using Peter.Common.MainMenu;

namespace Peter.Ruby.Tests
{
   /// <summary>
   /// Test cases for the rails console.
   /// </summary>
   [TestClass]
   public class RailsConsoleTests
   {
      /// <summary>
      /// Tests the constructor for the rails console.
      /// </summary>
      [TestMethod]
      public void TestRailsConsoleConstructor ()
      {
         var peter = new MockPeter ();
         var railsConsole = new RailsConsole ();

         var catalog = new AssemblyCatalog (Assembly.GetExecutingAssembly ());
         var container = new CompositionContainer (catalog);
         container.ComposeParts (peter, railsConsole);

         Assert.IsNotNull (railsConsole.Peter);
         Assert.IsNotNull (railsConsole.RunRailsServerCommand);
         Assert.IsNotNull (railsConsole.RunRailsServerContextMenuItem);
         Assert.IsNotNull (railsConsole.RunRailsConsoleContextMenuItem);
         Assert.AreEqual (MainMenuIcon.Play, railsConsole.RailsServerIcon);
         Assert.AreEqual ("Run Rails Server", railsConsole.RailsServerText);

         peter.TriggerShuttingDown ();
      }

      /// <summary>
      /// Tests the run rails server command.
      /// </summary>
      [TestMethod]
      public void TestRunRailsServerCommand ()
      {
         var peter = new MockPeter ();
         var railsConsole = new RailsConsole ();

         var catalog = new AssemblyCatalog (Assembly.GetExecutingAssembly ());
         var container = new CompositionContainer (catalog);
         container.ComposeParts (peter, railsConsole);

         Assert.IsFalse (railsConsole.RunRailsServerCommand.CanExecute (null));
         var menuItem = new PeterContextMenuItem { DataContext = MockProject };
         Assert.IsTrue (railsConsole.RunRailsServerCommand.CanExecute (menuItem));

         peter.TriggerShuttingDown ();
      }

      /// <summary>
      /// Gets or Sets the mocked peter project.
      /// </summary>
      protected static MockPeterProject MockProject { get; set; }

      /// <summary>
      /// Gets the test context.
      /// </summary>
      protected static TestContext Context { get; private set; }

      /// <summary>
      /// Initializes the tests in this class.
      /// </summary>
      /// <param name="context">Test context</param>
      [ClassInitialize]
      public static void ClassInitialize (TestContext context)
      {
         Context = context;
         var assem = Assembly.GetExecutingAssembly ();
         MockProject = new MockPeterProject (assem, context.TestDir);
      }

      /// <summary>
      /// Cleans up the tests in this class.
      /// </summary>
      [ClassCleanup]
      public static void ClassCleanup ()
      {
         MockProject.Close (Context.TestDir);
      }
   }
}
